{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/TexasInstruments/edgeai-tidl-tools/blob/experiment/examples/jupyter_notebooks/colab/infer_tflite.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RRlwdlGzUumd",
        "vscode": {
          "languageId": "python"
        }
      },
      "outputs": [],
      "source": [
        "!wget https://akm-img-a-in.tosshub.com/indiatoday/images/story/201804/jet.jpeg\n",
        "!wget https://tfhub.dev/tensorflow/lite-model/efficientnet/lite0/fp32/2?lite-format=tflite\n",
        "!mv /content/2?lite-format=tflite efficientnet-lit0.tflite\n",
        "!pip3 install tflite_runtime==2.8.0"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WsDWsaQKTTm3",
        "outputId": "301c0175-101f-408d-e8f1-5e5675645a46",
        "vscode": {
          "languageId": "python"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[895 403 744 657  51]\n"
          ]
        }
      ],
      "source": [
        "import tflite_runtime.interpreter as tflite\n",
        "import numpy as np\n",
        "import PIL.Image as Image\n",
        "\n",
        "model_path =\"./efficientnet-lit0.tflite\"\n",
        "input_file =\"jet.jpeg\"\n",
        "input_mean =127.5\n",
        "input_std  =127.5\n",
        "\n",
        "interpreter = tflite.Interpreter(model_path=model_path)\n",
        "interpreter.allocate_tensors()\n",
        "  \n",
        "input_details = interpreter.get_input_details()\n",
        "output_details = interpreter.get_output_details()\n",
        "\n",
        "#Pre-Process input\n",
        "height = input_details[0]['shape'][1]\n",
        "width = input_details[0]['shape'][2]\n",
        "img = Image.open(input_file).resize((width, height))\n",
        "input_data = np.expand_dims(img, axis=0)\n",
        "input_data = (np.float32(input_data) - input_mean) / input_std\n",
        "interpreter.set_tensor(input_details[0]['index'], input_data)\n",
        "\n",
        "#Run inference session\n",
        "interpreter.invoke()\n",
        "\n",
        "#Post Process Result\n",
        "output_data = interpreter.get_tensor(output_details[0]['index'])\n",
        "results = np.squeeze(output_data)\n",
        "print(results.argsort()[-5:][::-1])"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "authorship_tag": "ABX9TyOh4bkViU2xO4Jxe5GdhfiA",
      "include_colab_link": true,
      "name": "Infer-tflite.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
